package com.innoking.docsys.report.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.doc.module.UpmsArchivesTypesVO;
import com.innoking.docsys.doc.module.UpmsFondsManageVO;
import com.innoking.docsys.report.dao.cluster.ClusterCollectionMapperSon;
import com.innoking.docsys.report.feign.DocInterface;
import com.innoking.docsys.report.feign.UserRestInterface;
import com.innoking.docsys.report.module.CompreClassVO;
import com.innoking.docsys.report.service.CollectionService;
import com.innoking.docsys.utils.JsonToOtherUtil;
import com.innoking.docsys.utils.OtherPojo;
import com.innoking.docsys.utils.ResultObjVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CollectionServiceImpl
 * @Description 馆藏统计service
 * @Author wr
 * @Date 2019/9/4 000415:59
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    Logger logger= LoggerFactory.getLogger(CollectionServiceImpl.class);

    @Autowired
    private DocInterface docInterface;

    @Autowired
    private UserRestInterface userRestInterface;

    @Autowired
    private ClusterCollectionMapperSon clusterCollectionMapperSon;

    @Override
    public List<CompreClassVO> compreClass(String userId, String fondsId, String startDate, String endDate) {
        //获取全宗号做为查询条件
        String fondsNum = getUpmsArchivesTypesVOByFondsId(fondsId);
        ArrayList<CompreClassVO> compreClassVOS = new ArrayList<>();
        //获取档案分类
        List<UpmsArchivesTypesVO> upmsArchivesTypes = getUpmsArchivesTypes(Integer.valueOf(fondsId));
        for (UpmsArchivesTypesVO upmsArchivesType : upmsArchivesTypes) {
            CompreClassVO compreClassVO = new CompreClassVO();
            compreClassVO.setSortNum(String.valueOf(upmsArchivesType.getSortNum()));
            compreClassVO.setSortName(String.valueOf(upmsArchivesType.getSortName()));

            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("fondsNum",fondsNum);
            stringObjectHashMap.put("sortNum",String.valueOf(upmsArchivesType.getSortNum()));
            stringObjectHashMap.put("startDate",startDate);
            stringObjectHashMap.put("endDate",endDate);
            //1.归档数
            //1.1.获取归档案卷数
            List<Map<String, Object>> volByCount = clusterCollectionMapperSon.getVolByCount(stringObjectHashMap);
            Integer gd_aj = Integer.valueOf(volByCount.get(0).get("COUNTS").toString());
            compreClassVO.setGd_aj(gd_aj);
            //1.2.获取归档单件数
            List<Map<String, Object>> sinItemByCount = clusterCollectionMapperSon.getSinItemByCount(stringObjectHashMap);
            Integer gd_dj = Integer.valueOf(sinItemByCount.get(0).get("COUNTS").toString());
            compreClassVO.setGd_dj(gd_dj);
            //1.3.归档数小计
            Integer gd_xj=gd_aj+gd_dj;
            compreClassVO.setGd_xj(gd_xj);

            //2.借阅数
            //2.1借阅案卷数
            List<Map<String, Object>> jy_aj_count = clusterCollectionMapperSon.get_JY_AJ_Count(stringObjectHashMap);
            Integer jy_aj = Integer.valueOf(jy_aj_count.get(0).get("COUNTS").toString());
            compreClassVO.setJy_aj(jy_aj);
            //2.2借阅单件数
            List<Map<String, Object>> jy_dj_count = clusterCollectionMapperSon.get_JY_DJ_Count(stringObjectHashMap);
            Integer jy_dj = Integer.valueOf(jy_dj_count.get(0).get("COUNTS").toString());
            compreClassVO.setJy_dj(jy_dj);
            //2.3 借阅小计
            Integer jy_xj=jy_aj+jy_dj;
            compreClassVO.setJy_xj(jy_xj);

            //3.鉴定数
            //3.1鉴定案卷数
            List<Map<String, Object>> jd_aj_count = clusterCollectionMapperSon.get_JD_AJ_Count(stringObjectHashMap);
            Integer jd_aj = Integer.valueOf(jd_aj_count.get(0).get("COUNTS").toString());
            compreClassVO.setJd_aj(jd_aj);
            //3.2鉴定单件数
            List<Map<String, Object>> jd_dj_count = clusterCollectionMapperSon.get_JD_DJ_Count(stringObjectHashMap);
            Integer jd_dj = Integer.valueOf(jd_dj_count.get(0).get("COUNTS").toString());
            compreClassVO.setJd_dj(jd_dj);
            //3.3鉴定小计
            Integer jd_xj=jd_aj+jd_dj;
            compreClassVO.setJd_xj(jd_xj);

            //4.销毁数
            //4.1销毁案卷数
            List<Map<String, Object>> xh_aj_count = clusterCollectionMapperSon.get_XH_AJ_Count(stringObjectHashMap);
            Integer xh_aj = Integer.valueOf(xh_aj_count.get(0).get("COUNTS").toString());
            compreClassVO.setXh_aj(xh_aj);
            //4.2销毁单件数
            List<Map<String, Object>> xh_dj_count = clusterCollectionMapperSon.get_XH_DJ_Count(stringObjectHashMap);
            Integer xh_dj = Integer.valueOf(xh_dj_count.get(0).get("COUNTS").toString());
            compreClassVO.setXh_dj(xh_dj);
            //4.3销毁小计
            Integer xh_xj=xh_aj+xh_dj;
            compreClassVO.setXh_xj(xh_xj);
            compreClassVOS.add(compreClassVO);
        }
        return compreClassVOS;
    }

    /**
     * 传入组织机构Code获取档案分类List
     * @param orgCode 组织机构code
     * @return
     */
    public  List<UpmsArchivesTypesVO> getUpmsArchivesTypesByAll(String orgCode){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("orgCode",orgCode);
        String dataValue = JsonToOtherUtil.packageEntering(stringObjectHashMap);
        String classByFoundId = userRestInterface.getFondsByOrgCode(dataValue);
        ResultObjVO resultObjVO = JsonToOtherUtil.rtResultObjVO(classByFoundId, new UpmsFondsManageVO());
        UpmsFondsManageVO obj = (UpmsFondsManageVO) resultObjVO.getObj();
        Integer fondsId = obj.getFondsId();
        //通过全宗ID，获取全宗下档案分类
        List<UpmsArchivesTypesVO> upmsArchivesTypes = this.getUpmsArchivesTypes(fondsId);
        return upmsArchivesTypes;
    };

    /**
     * 传入全宗id获取档案分类List
     * @param fondsId
     * @return
     */
    public List<UpmsArchivesTypesVO> getUpmsArchivesTypes(Integer fondsId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("id",fondsId);
        String dataValue = JsonToOtherUtil.packageEntering(stringObjectHashMap);
        String classByFoundId = docInterface.getClassByFoundId(dataValue);
        OtherPojo otherPojo = JsonToOtherUtil.rtOtherString(classByFoundId);
        Object obj = otherPojo.getObj();
        List<UpmsArchivesTypesVO> upmsArchivesTypesVOS = JSONArray.parseArray(obj.toString(), UpmsArchivesTypesVO.class);
        return upmsArchivesTypesVOS;
    }

    /**
     * 传入全宗id获取全宗号
     * @param fondsId
     * @return
     */
    public String getUpmsArchivesTypesVOByFondsId(String fondsId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("id",fondsId);
        String dataValue = JsonToOtherUtil.packageEntering(stringObjectHashMap);
        String classByFoundId = userRestInterface.getFondsPojoByFondsId(dataValue);
        OtherPojo otherPojo = JsonToOtherUtil.rtOtherString(classByFoundId);
        Object obj = otherPojo.getObj();
        UpmsFondsManageVO upmsFondsManageVO = JSONObject.parseObject(obj.toString(), UpmsFondsManageVO.class);
        String fondsNum = upmsFondsManageVO.getFondsNum();
        return fondsNum;
    }


}
